//
//  MNDEmailAuthViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/8.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDEmailAuthViewController.h"
#import "NSString+JKNormalRegex.h"
#import "UIImage+JKColor.h"

@interface MNDEmailAuthViewController ()


@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (copy, nonatomic) NSString *email;

@end

@implementation MNDEmailAuthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.submitBtn setBackgroundImage:[UIImage imageNamed:@"invest_result_continue_btn"] forState:UIControlStateNormal];
    [self.submitBtn setBackgroundImage:[UIImage jk_imageWithColor:kColorLightGray] forState:UIControlStateDisabled];
    
    RAC(self, email) = self.emailTextField.rac_textSignal;
    RAC(self.submitBtn, enabled) = [RACSignal combineLatest:@[RACObserve(self, email)] reduce:^id(NSString *x){
        
        return @(x.length > 0);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)submitAction:(id)sender {
    
    if (!self.email.jk_isEmailAddress) {
        [NetworkProgressHUD showHudMsg:@"请输入正确的邮箱地址"];
        return;
    }
    [NetworkProgressHUD showHud];
    WeakSelf(self);
    if (self.isAddEmail) {
        NSDictionary *params = [MNDSign signedParamWithDict:@{@"user_email": self.email}];
        
        [APIManager sendRequestWithPath:API_ADD_EMAIL parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
            if (responseModel.status == ResponseSuccess) {
                
                [NetworkProgressHUD showHudMsg:@"邮件已发送，请到您的邮箱去激活"];
                [weakself performSelector:@selector(popToRootVC) withObject:nil afterDelay:2.0];
            } else {
                NSDictionary *dict = responseModel.msg;
                if (dict && dict.count > 0) {
                    [NetworkProgressHUD showHudMsg:dict[@"error"]];
                    
                } else {
                    [NetworkProgressHUD showHudMsg:@"获取数据失败"];
                }
            }
        } onFailure:^(NSError *error) {
            [NetworkProgressHUD showHudNetworkError:error];
        }];
    } else {
        NSDictionary *params = [MNDSign signedParamWithDict:@{@"user_email": self.email}];
        
        [APIManager sendRequestWithPath:API_UPDATE_EMAIL parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
            if (responseModel.status == ResponseSuccess) {
                
                [NetworkProgressHUD showHudMsg:@"邮件已发送，请到您的邮箱去激活"];
                [weakself performSelector:@selector(popToRootVC) withObject:nil afterDelay:2.0];
            } else {
                NSDictionary *dict = responseModel.msg;
                if (dict && dict.count > 0) {
                    [NetworkProgressHUD showHudMsg:dict[@"error"]];
                    
                } else {
                    [NetworkProgressHUD showHudMsg:@"获取数据失败"];
                }
            }
        } onFailure:^(NSError *error) {
            [NetworkProgressHUD showHudNetworkError:error];
        }];
    }
}

- (void)popToRootVC {
    UIViewController *vc = self.navigationController.viewControllers[1];
    [self.navigationController popToViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
